package com.dora.basic.juc.aqs;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Aqs例子
 *
 * @author hanchen
 * @date 2024/02/21
 */
public class AqsExample {

    private static final Lock LOCK = new ReentrantLock();

    public static void main(String[] args) {
        // 创建两个线程，并启动
        Thread thread1 = new Thread(AqsExample::doSomething);
        Thread thread2 = new Thread(AqsExample::doSomething);
        thread1.start();
        thread2.start();
    }

    public static void doSomething() {
        LOCK.lock(); // 获取锁
        try {
            System.out.println(Thread.currentThread().getName() + " is doing something.");
            // 模拟做一些事情的时间
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            LOCK.unlock(); // 释放锁
        }
    }
}
